//==================================================================================================
//| 文件名称 | bsp_converter.c
//|--------- |--------------------------------------------------------------------------------------
//| 文件描述 | 版级数据类型转换
//|--------- |--------------------------------------------------------------------------------------
//| 运行环境 |
//|--------- |--------------------------------------------------------------------------------------
//| 版权声明 |
//|----------|--------------------------------------------------------------------------------------
//|  版本    |  时间       |  作者     | 描述
//|--------- |-------------|-----------|------------------------------------------------------------
//|  V1.0    | 2018.10.23  |  wjb      | 初版 TI28377D平台 应该是适用于所有平台的
//==================================================================================================
#include <bsp/bsp_converter.h>


/*
举一个例子，比如数字0x12 34 56 78在内存中的表示形式。

1)大端模式：Big-Endian就是高位字节排放在内存的低地址端，低位字节排放在内存的高地址端。

　　　　　　　　　　　　（其实大端模式才是我们直观上认为的模式，和字符串存储的模式差类似）

低地址 --------------------> 高地址
0x12  |  0x34  |  0x56  |  0x78

2)小端模式：Little-Endian就是低位字节排放在内存的低地址端，高位字节排放在内存的高地址端。

低地址 --------------------> 高地址
0x78  |  0x56  |  0x34  |  0x12

*/


uint16_t bsp_cnv_arr_to_uint16(uint8_t *puc_Arr,bool b_Rev)
{
    uint16_t i = 0;
    if(b_Rev == false)
    {
        i = (((uint32_t)puc_Arr[1]<<8) + ((uint32_t)puc_Arr[0]));
    }
    else
    {
        i = (((uint32_t)puc_Arr[0]<<8) + ((uint32_t)puc_Arr[1]));
    }
    return i;
}

void bsp_cnv_uint16_to_arr(uint8_t *puc_Arr, uint16_t uin_Data, bool b_Rev)
{
    uint16_t i = uin_Data;
    if(b_Rev == false)
    {
        puc_Arr[1] = (uint8_t)(i>>8)&0xff;
        puc_Arr[0] = (uint8_t)(i&0xff);
    }
    else
    {
        puc_Arr[0] = (uint8_t)(i>>8)&0xff;
        puc_Arr[1] = (uint8_t)(i&0xff);
    }
}

uint32_t bsp_cnv_arr_to_uint32(uint8_t *puc_Arr, bool b_Rev)
{
    uint32_t i = 0;
    if(b_Rev == false)
    {
        i = (((uint32_t)puc_Arr[3]<<24) + ((uint32_t)puc_Arr[2]<<16) +
            ((uint32_t)puc_Arr[1]<<8) + ((uint32_t)puc_Arr[0]));
    }
    else
    {
        i = (((uint32_t)puc_Arr[0]<<24) + ((uint32_t)puc_Arr[1]<<16) +
            ((uint32_t)puc_Arr[2]<<8) + ((uint32_t)puc_Arr[3]));
    }
    return i;
}

void bsp_cnv_uint32_to_arr(uint8_t *puc_Arr,uint32_t ul_Data,bool b_Rev)
{
    uint32_t i = ul_Data;
    if(b_Rev == false)
    {
        puc_Arr[3] = (uint8_t)(i>>24)&0xff;
        puc_Arr[2] = (uint8_t)(i>>16)&0xff;
        puc_Arr[1] = (uint8_t)(i>>8)&0xff;
        puc_Arr[0] = (uint8_t)(i&0xff);
    }
    else
    {
        puc_Arr[0] = (uint8_t)(i>>24)&0xff;
        puc_Arr[1] = (uint8_t)(i>>16)&0xff;
        puc_Arr[2] = (uint8_t)(i>>8)&0xff;
        puc_Arr[3] = (uint8_t)(i&0xff);
    }
}

float_t bsp_cnv_arr_to_float(uint8_t* puc_Arr, bool b_Rev)
{
    float_t f_Temp;
    uint32_t i;
    if(b_Rev == false)
    {
        i = (((uint32_t)puc_Arr[3]<<24) + ((uint32_t)puc_Arr[2]<<16) +
            ((uint32_t)puc_Arr[1]<<8) + ((uint32_t)puc_Arr[0]));
    }
    else
    {
        i = (((uint32_t)puc_Arr[0]<<24) + ((uint32_t)puc_Arr[1]<<16) +
            ((uint32_t)puc_Arr[2]<<8) + ((uint32_t)puc_Arr[3]));
    }
    f_Temp = *((float_t*)&i);
    return f_Temp;
}

void bsp_cnv_float_to_arr(uint8_t* puc_Arr, float_t f_Data, bool b_Rev)
{
    uint32_t i = *((uint32_t*)&f_Data);
    if(b_Rev == false)
    {
        puc_Arr[3] = (uint8_t)(i>>24)&0xff;
        puc_Arr[2] = (uint8_t)(i>>16)&0xff;
        puc_Arr[1] = (uint8_t)(i>>8)&0xff;
        puc_Arr[0] = (uint8_t)(i&0xff);
    }
    else
    {
        puc_Arr[0] = (uint8_t)(i>>24)&0xff;
        puc_Arr[1] = (uint8_t)(i>>16)&0xff;
        puc_Arr[2] = (uint8_t)(i>>8)&0xff;
        puc_Arr[3] = (uint8_t)(i&0xff);
    }
}

double_t bsp_cnv_arr_to_double(uint8_t* puc_Arr,bool b_Rev)
{
    double_t lf_Temp;
    uint64_t i;
    if(b_Rev == false)
    {
        i = (((uint64_t)puc_Arr[7]<<56) + ((uint64_t)puc_Arr[6]<<48) +
            ((uint64_t)puc_Arr[5]<<40) + ((uint64_t)puc_Arr[4]<<32) +
            ((uint64_t)puc_Arr[3]<<24) + ((uint64_t)puc_Arr[2]<<16) +
            ((uint64_t)puc_Arr[1]<<8) + ((uint64_t)puc_Arr[0]));
    }
    else
    {
        i = (((uint64_t)puc_Arr[0]<<56) + ((uint64_t)puc_Arr[1]<<48) +
            ((uint64_t)puc_Arr[2]<<40) + ((uint64_t)puc_Arr[3]<<32) +
            ((uint64_t)puc_Arr[4]<<24) + ((uint64_t)puc_Arr[5]<<16) +
            ((uint64_t)puc_Arr[6]<<8) + ((uint64_t)puc_Arr[7]));
    }
    lf_Temp = *((double_t*)&i);
    return lf_Temp;
}

void bsp_cnv_double_to_arr(uint8_t* puc_Arr, double_t lf_Data,bool b_Rev)
{
    uint64_t i = *((uint64_t*)&lf_Data);
    if(b_Rev == false)
    {
        puc_Arr[7] = (uint8_t)(i>>56)&0xff;
        puc_Arr[6] = (uint8_t)(i>>48)&0xff;
        puc_Arr[5] = (uint8_t)(i>>40)&0xff;
        puc_Arr[4] = (uint8_t)(i>>32)&0xff;
        puc_Arr[3] = (uint8_t)(i>>24)&0xff;
        puc_Arr[2] = (uint8_t)(i>>16)&0xff;
        puc_Arr[1] = (uint8_t)(i>>8)&0xff;
        puc_Arr[0] = (uint8_t)(i&0xff);
    }
    else
    {
        puc_Arr[0] = (uint8_t)(i>>56)&0xff;
        puc_Arr[1] = (uint8_t)(i>>48)&0xff;
        puc_Arr[2] = (uint8_t)(i>>40)&0xff;
        puc_Arr[3] = (uint8_t)(i>>32)&0xff;
        puc_Arr[4] = (uint8_t)(i>>24)&0xff;
        puc_Arr[5] = (uint8_t)(i>>16)&0xff;
        puc_Arr[6] = (uint8_t)(i>>8)&0xff;
        puc_Arr[7] = (uint8_t)(i&0xff);
    }
}


